/**
 * 
 */
package au.com.betaversion.patientrecords.service;

import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.springframework.stereotype.Service;

import au.com.betaversion.patientrecords.domain.Age;

/**
 * @author Maciej Radochonski
 *
 */
@Service
public class DateServiceImpl implements DateService {

	private Age age;
	
	/* (non-Javadoc)
	 * @see au.com.betaversion.patientrecords.service.PatientService#calculateAge(java.util.Date)
	 */
	public Age calculateAge(Date dateOfBirth) {
		this.age = new Age();
		
		DateTime birthDate = new DateTime(dateOfBirth.getTime());
		DateTime now = new DateTime();
		
		Interval interval = new Interval(birthDate, now);

		Period period = interval.toPeriod(PeriodType.yearMonthDay());
		age.setYear(period.getYears());
		age.setMonth(period.getMonths());
		age.setDay(period.getDays());
		
		return age;
	}
	
}
